/**
 * 
 */
package earserpdf;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PRStream;
import com.itextpdf.text.pdf.PdfDictionary;
import com.itextpdf.text.pdf.PdfImage;
import com.itextpdf.text.pdf.PdfName;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfStream;
import com.itextpdf.text.pdf.parser.PdfImageObject;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * <p>Title:ReplaceImageInPdf</p>
 * <p>Description:convert colored images to black and white</p>
 * @author	xiongf
 * @date	2016年3月31日 下午2:01:08
 * @version 1.0
 */
public class ConvertImageColorInPdf {
	public static final String SRC = "E:/Workspace/ItextPDFDemo/pdffolder/pdf_image.pdf";
	public static final String DEST = "E:/Workspace/ItextPDFDemo/pdffolder/207_DEPD1DI0_20160229_.pdf";

	public static void main(String[] args) throws DocumentException, IOException {
		File file = new File(DEST);
		file.getParentFile().mkdirs();
		new ConvertImageColorInPdf().manipulatePdf(SRC, DEST);
	}

	public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
		PdfReader reader = new PdfReader(src);
		PdfDictionary page = reader.getPageN(1);
		PdfDictionary resources = page.getAsDict(PdfName.RESOURCES);
		PdfDictionary xobjects = resources.getAsDict(PdfName.XOBJECT);
		PdfName imgRef = xobjects.getKeys().iterator().next();
		PRStream stream = (PRStream) xobjects.getAsStream(imgRef);
		Image img = makeBlackAndWhitePng(new PdfImageObject(stream));
		PdfImage image = new PdfImage(makeBlackAndWhitePng(new PdfImageObject(stream)), "", null);
		replaceStream(stream, image);
		PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
		stamper.close();
		reader.close();
	}

	public static Image makeBlackAndWhitePng(PdfImageObject image) throws IOException,
			DocumentException {
		BufferedImage bi = image.getBufferedImage();
		BufferedImage newBi = new BufferedImage(bi.getWidth(), bi.getHeight(),
				BufferedImage.TYPE_USHORT_GRAY);
		newBi.getGraphics().drawImage(bi, 0, 0, null);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ImageIO.write(newBi, "png", baos);
		return Image.getInstance(baos.toByteArray());
	}

	public static void replaceStream(PRStream orig, PdfStream stream) throws IOException {
		orig.clear();
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		stream.writeContent(baos);
		orig.setData(baos.toByteArray(), false);
		for (PdfName name : stream.getKeys()) {
			orig.put(name, stream.get(name));
		}
	}
}
